/**
 * @description user services
 */

const {
    User,
    Follow
} = require('../db/model/index')
/**
 * @description 创建用户
 * @param {*} param0 
 */
async function createUser({
    userName,
    gender,
    avatar,
    openId,
    sessionKey
}) {
    const result = await User.create({
        userName,
        gender,
        avatar,
        openId,
        sessionKey
    })
    console.log(JSON.stringify(result))
    const data = result.dataValues
    return data
}

/**
 * 查询用户
 * @param {*} param0 
 */
async function getUserList({
    province,
    city,
    types,
    pageIndex,
    pageSize
}) {
    let whereData = {
        "auth": 1 //必须认证过的用户
    }
    if (province != '全国') {
        whereData.province = province
    }

    if (city != '全部') {
        whereData.city = city
    }

    if(types != '全部'){
        whereData.types = types
    }
    const result =await User.findAndCountAll({
        limit: pageSize,//每次查询多少条
        offset:pageSize * pageIndex,//跳过多少条
        attributes: ['id', 'userName', 'nickName', 'avatar', 'cover', 'types'],
        order: [
            ['id', 'desc']
        ],
        where: whereData
    })
    return result
}

/**
 * @description 更新用户信息
 * @param {*} param0 
 */
async function updateUser({
    cover,
    nickName,
    describe,
    city,
    province,
    types,
    weChat,
    phone,
    imgsData,
    auth,
    openId
}) {

    // 拼接修改内容
    let updateData = {}
    if (cover) {
        updateData.cover = cover
    }
    if (nickName) {
        updateData.nickName = nickName
    }
    if (describe) {
        updateData.describe = describe
    }
    if (city) {
        updateData.city = city
    }
    if (province) {
        updateData.province = province
    }
    if (types) {
        updateData.types = types
    }
    if (weChat) {
        updateData.weChat = weChat
    }
    if (phone) {
        updateData.phone = phone
    }
    if (imgsData) {
        updateData.imgsData = imgsData
    }
    if (auth) {
        updateData.auth = auth
    }

    const result = await User.update(updateData, {
        where: {
            openId
        }
    })
    return result[0] > 0 // 修改的行数
}

/**
 * @description 根据openId 查询用户信息
 * @param {string} id 用户的openId
 */
async function getUserInfo({id,openId}) {
    let whereData = {}
    if(id){
        whereData.id = id
    }
    if(openId){
        whereData.openId = openId
    }
    const result =await User.findOne({
        attributes: ['auth','id','avatar','userName'],
        where: whereData
    })
    return result
}

/**
 * @description 查询用户详细信息
 * @param {*} id 用户Id
 */
async function getUserInfoDetail(id) {
    const result = User.findOne({
        attributes: ['id', 'cover', 'avatar', 'userName', 'types', 'province', 'city', 'describe', 'imgsData'],
        where: {
            id
        }
    })
    return result
}
module.exports = {
    createUser,
    updateUser,
    getUserInfo,
    getUserList,
    getUserInfoDetail
}